<?php

namespace App\Http\Controllers\App;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Role;
use App\Models\Action;
use App\Http\Controllers\App\BaseController;

class RoleController extends BaseController
{

    /**
     * 角色列表
     * @return [type] [description]
     */
    public function roleList(Request $request) 
    {

        $name   = $request->input('name');

        $status = $request->input('status');

        $roleModel = new Role();

        if ($name != null) {
            $roleModel = $roleModel->where('name', 'like', '%'.$name.'%');
        }

        if ($status != null) {
            $roleModel = $roleModel->where('status', $status);
        }

        $result = $roleModel->paginate($this->pageSize);

        return self::json($result);
    }

    /**
     * 角色列表
     * @return [type] [description]
     */
    public function roleOne(Request $request) 
    {

        $id = intval($request->input('id'));

        if ($id == 0) return self::error('id有误');

        $result  = Role::find($id);

        return empty($result) ? self::error('id有误') : self::json($result);
    }

    /**
     * 角色权限列表
     * @return [type] [description]
     */
    public function roleActionList(Request $request) 
    {

        $id = intval($request->input('id'));

        if ($id == 0) {
            return self::error('id有误');
        }

        $RoleModel      = Role::find($id);

        if(empty($RoleModel)) {

            return self::error('id有误');
        }

        //获得权限列表（用于编辑权限界面）
        $action = new Action();
        $actionList = $action->ActionList($RoleModel->actionids);

        return self::json($actionList);
    }

    /**
     * 角色添加
     * @return [type] [description]
     */
    public function roleAdd(Request $request) 
    {

        $params = [];

        $name = $request->input('name');
            
        if ($name == null) self::error('角色名字不能为空');

        $request->input('actionids')    == null || $params['actionids']    = $request->input('actionids');
        $request->input('displayorder') == null || $params['displayorder'] = $request->input('displayorder');
        $request->input('status')       == null || $params['status']       = $request->input('status');
        
        $params['name'] = $name;

        $RoleModel = new Role();
        //对象赋值
        $RoleModel->setAttributes($params);

        $result = $RoleModel->save();

        return ($result == false) ? self::error('添加失败') : self::success('添加成功');
    }

    /**
     * 角色数据编辑
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function roleEdit(Request $request) 
    {

        $params = [];

        $request->input('name')         == null || $params['name']          = $request->input('name');
        $request->input('status')       == null || $params['status']        = $request->input('status');
        $request->input('actionids')    == null || $params['actionids']     = $request->input('actionids');
        $request->input('displayorder') == null || $params['displayorder']  = $request->input('displayorder');

        $id = intval($request->input('id'));

        if ($id == 0) return self::error('id有误');

        if (count($params) == 0) return self::success('更新成功');

        $RoleModel = Role::find($id);

        if(empty($RoleModel)) return self::error('id有误');

        $RoleModel->setAttributes($params);

        $return = $RoleModel->save();

        return ($result == false) ? self::error('编辑失败') : self::success('编辑成功');
    }

    /**
     * 角色添加权限
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function roleActionEdit(Request $request) 
    {

        $actionids = $request->input('actionids','');
 
        $id = intval($request->input('id'));

        if ($id == 0) return self::error('id有误');

        $RoleModel = Role::find($id);

        if(empty($RoleModel)) return self::error('id有误');

        $RoleModel->actionids = $actionids;

        $result = $RoleModel->save();

        return ($result == false) ? self::error('权限编辑失败') : self::success('权限编辑成功');
    }

    /**
     * 角色删除
     * @return [type] [description]
     */
    public function roleDelete(Request $request) 
    {
        
        $id = intval($request->input('id'));

        if ($id == 0) return self::error('id有误');

        $RoleModel = Role::find($id);

        if(empty($RoleModel)) return self::error('id有误');

        $RoleModel->isdelete = Role::IS_DELETE;

        $result = $RoleModel->save();

        return ($result == false) ? self::error('删除失败') : self::success('删除成功');
    }
}
